package cn.azr.infrastructure.persistent.repository;



import cn.azr.domain.strategy.model.entity.StrategyAwardEntity;
import cn.azr.domain.strategy.model.entity.StrategyEntity;
import cn.azr.domain.strategy.model.entity.StrategyRuleEntity;
import cn.azr.domain.strategy.model.vo.StrategyAwardRuleModelVO;
import cn.azr.domain.strategy.repository.IStrategyRepository;
import cn.azr.infrastructure.persistent.dao.IStrategyAwardDao;
import cn.azr.infrastructure.persistent.dao.IStrategyDao;
import cn.azr.infrastructure.persistent.dao.IStrategyRuleDao;
import cn.azr.infrastructure.persistent.po.Strategy;
import cn.azr.infrastructure.persistent.po.StrategyAward;
import cn.azr.infrastructure.persistent.po.StrategyRule;
import cn.azr.infrastructure.persistent.redis.IRedisService;
import cn.azr.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;


import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * ClassName:StrategyRepository
 * Description:
 *
 * @version 1.0
 * @Author azr
 * @Create 2024/9/28 10:26
 */
@Slf4j
@Repository
public class StrategyRepository implements IStrategyRepository {

    @Resource
    private IStrategyDao iStrategyDao;
    @Resource
    private IStrategyAwardDao iStrategyAwardDao;

    @Resource
    private IStrategyRuleDao iStrategyRuleDao;
    @Resource
    private IRedisService iRedisService;




    @Override
    public List<StrategyAwardEntity> queryStrategyIdList(Long strategyId) {
        //1.首先查询缓存，构建cacheKey,查询返回StrategyAwardEntity
        String cacheKey = Constants.RedisKey.STRATEGY_AWARD_KEY + strategyId;
        List<StrategyAwardEntity> strategyAwardEntities = iRedisService.getValue(cacheKey);
        if (null != strategyAwardEntities && !strategyAwardEntities.isEmpty()) {
            return strategyAwardEntities;
        }
        //2.缓存查询不到，走数据库查询,查询返回类型为po
        List<StrategyAward> strategyAwards = iStrategyAwardDao.queryAwardByStrategyId(strategyId);
        strategyAwardEntities = new ArrayList<>(strategyAwards.size());
        for (StrategyAward strategyAward : strategyAwards) {
            StrategyAwardEntity strategyAwardEntity = StrategyAwardEntity.builder()
                    .strategyId(strategyAward.getStrategyId())
                    .awardId(strategyAward.getAwardId())
                    .awardCount(strategyAward.getAwardCount())
                    .awardCountSurplus(strategyAward.getAwardCountSurplus())
                    .awardRate(strategyAward.getAwardRate())
                    .build();
            strategyAwardEntities.add(strategyAwardEntity);
        }
        //3.数据库查询到同时写入缓存
        iRedisService.setValue(cacheKey, strategyAwardEntities);
        return strategyAwardEntities;
    }

    @Override
    public void store(String key, BigDecimal awardRateRange, Map<Integer, Integer> shufflestrategyAwardRateTables) {
        // 1. 存储抽奖策略范围值，如10000，用于生成1000以内的随机数
        iRedisService.setValue(Constants.RedisKey.STRATEGY_RATE_RANGE_KEY + key, awardRateRange.intValue());
        // 2. 存储概率查找表
        Map<Integer, Integer> cacheRateTable = iRedisService.getMap(Constants.RedisKey.STRATEGY_RATE_TABLE_KEY + key);
        cacheRateTable.putAll(shufflestrategyAwardRateTables);
    }

    @Override
    public int getRangeRate(Long strategyId) {
        return getRateRange(String.valueOf(strategyId));
    }
    @Override
    public int getRateRange(String key) {
        return iRedisService.getValue(Constants.RedisKey.STRATEGY_RATE_RANGE_KEY + key);
    }


    @Override
    public int getStrategyAwardAssemble(String key, int rateKey) {
        return iRedisService.getFromMap(Constants.RedisKey.STRATEGY_RATE_TABLE_KEY + key, rateKey);
    }

    @Override
    public StrategyEntity queryEntityByStrategyId(Long strategyId) {
        //1.查询缓存
        String cacheKey = Constants.RedisKey.STRATEGY_KEY + strategyId;
        StrategyEntity strategyEntity = iRedisService.getValue(cacheKey);
        if (null != strategyEntity) {
            return strategyEntity;
        }

        //2.查询数据库
        Strategy strategy = iStrategyDao.queryStrategyEntity(strategyId);

        StrategyEntity entity = StrategyEntity.builder()
                .strategyId(strategy.getStrategyId())
                .strategyDesc(strategy.getStrategyDesc())
                .ruleModel(strategy.getRuleModels())
                .build();

        //3.写入缓存后返回
        iRedisService.setValue(cacheKey,entity);
        return  entity;


    }

    @Override
    public StrategyRuleEntity queryStrategyRule(Long strategyId, String ruleModel) {
        StrategyRule strategyRuleReq = new StrategyRule();
        strategyRuleReq.setStrategyId(strategyId);
        strategyRuleReq.setRuleModel(ruleModel);
        StrategyRule strategyRuleRes = iStrategyRuleDao.queryStrategyRule(strategyRuleReq);
        return StrategyRuleEntity.builder()
                .strategyId(strategyRuleRes.getStrategyId())
                .awardId(strategyRuleRes.getAwardId())
                .ruleType(strategyRuleRes.getRuleType())
                .ruleModel(strategyRuleRes.getRuleModel())
                .ruleValue(strategyRuleRes.getRuleValue())
                .ruleDesc(strategyRuleRes.getRuleDesc())
                .build();
    }

    @Override
    public String queryStrategyRuleValue(Long strategyId, Integer awardId, String ruleModel) {
        StrategyRule strategyRule = new StrategyRule();
        strategyRule.setStrategyId(strategyId);
        strategyRule.setAwardId(awardId);
        strategyRule.setRuleModel(ruleModel);
        return iStrategyRuleDao.queryStrategyRuleValue(strategyRule);
    }

    @Override
    public String queryStrategyRuleValue(Long strategyId, String ruleModel) {
        return queryStrategyRuleValue(strategyId,null,ruleModel);
    }

    @Override
    public StrategyAwardRuleModelVO queryStrategyAwardRuleModelVO(Long strategyId, Integer awardId) {
        StrategyAward strategyAward = new StrategyAward();
        strategyAward.setStrategyId(strategyId);
        strategyAward.setAwardId(awardId);
        String ruleModels = iStrategyAwardDao.queryStrategyAwardRuleModels(strategyAward);
        return StrategyAwardRuleModelVO.builder().ruleModels(ruleModels).build();
    }


}

